var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data: {
    companyId:0,
    address:"请选择用印位置",
    selectSealName:"请选择关联印章",
    showSealDialog:false,
    latitude: "",
    longitude: "",
    radius:500,
    markers: [{
      id:0,
      iconPath: "../../../images/workbench/position.png",
      latitude: "",
      longitude: "",
      width:32,
      height: 32
    }],
    circles: [{
      latitude: "",
      longitude: "",
      color: '#7cb5ec',
      fillColor: '#7cb5ec88',
      radius: 1,
      strokeWidth: 1
    }],
    sealList:[],
    selectSealIds:[],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '新建电子围栏', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad: function () {
    requestClient = new ZYRequestClient();
    this.getLocation();
  },
  // 选择位置
  chooseLocation:function(){
    let that = this;
    wx.chooseLocation({
      success(res){
        that.setData({
          latitude:res.latitude,
          longitude:res.longitude,
          address:res.address,
          'markers[0].latitude':res.latitude,
          'markers[0].longitude':res.longitude,
          'circles[0].latitude':res.latitude,
          'circles[0].longitude':res.longitude,
          'circles[0].radius':that.data.radius,
        });
      }
    })
  },
  // 获取当前位置经纬度(页面只加载一次)
  getLocation:function(){
    var _this =this;
    wx.getLocation({
        type: 'gcj02',
      })
      .then(res => {
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      })
      .catch(err => {
        wx.showModal({
          title: "提示",
          content: "获取定位信息失败",
          showCancel: false
        })
      })
  },
  onShow: function () {
    requestClient.checkFunctionPermission(1005,'您没有权限添加电子围栏，请联系企业创建者！',function(){},function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 加载公司印章列表
  loadCompanySealList(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany');
    if(defaultCompany == ''){
      defaultCompany = { id: 0 };
    }
    _this.setData({
      companyId:defaultCompany.id
    });
    requestClient.getSealList(defaultCompany.id,function(res){
      _this.setData({
        sealList:res.data.data
      });
    });
  },
  // 监听范围值变化
  bindRangeChanging(e){
    var _this = this;
    _this.setData({
      radius:e.detail.value
    });
    // 设置范围值
    _this.setData({
      'circles[0].radius':_this.data.radius
    });
  },
  // 显示选择框
  bindShowSealClick(e){
    var _this = this;
    _this.setData({
      showSealDialog:true
    });
    _this.loadCompanySealList();
  },
  // 取消则隐藏选择框
  bindCancelSelectClick(e){
    var _this = this;
    _this.setData({
      showSealDialog:false
    });
  },
  // 确认选择
  bindSelectedSealClick(e){
    var _this = this;
    _this.setData({
      showSealDialog:false,
      selectSealName:'请选择关联印章'
    });
    
    // 拼接选中的印章
    var selectSealName = [];
    for(var i=0;i<_this.data.selectSealIds.length;i++){
      var seal = _this.data.sealList.find(seal=>seal.id == _this.data.selectSealIds[i]);
      var name = seal.name;
      selectSealName.push(name);
    }
    if(selectSealName.length > 0){
      _this.setData({
        selectSealName:selectSealName.join(',')
      });
    }
  },
  // 选中事件
  checkboxChange(e){
    // 赋值选中的印章id
    this.setData({
      selectSealIds:[e.detail.value]
    });
  },
  // 提交
  bindSave(e){
    var _this = this;
    // 判断是否选择位置
    if(_this.data.address == '请选择用印位置'){
      wx.showToast({
        title: '请选择用印位置!',
        icon:'error',
        mask:true
      })
      return;
    }
    // 判断是否选择印章
    if(_this.data.selectSealName == '请选择关联印章'){
      wx.showToast({
        title: '请选择关联印章!',
        icon:'error',
        mask:true
      })
      return;
    }
    // 构造电子围栏数据
    var userInfo = wx.getStorageSync('userInfo');
    var electronicFenceData = {
      companyId: _this.data.companyId,
      latitude:_this.data.latitude,
      longitude:_this.data.longitude,
      radius:_this.data.radius,
      address:_this.data.address,
      createUser:userInfo.id,
      sealIdList:_this.data.selectSealIds
    };
    requestClient.checkFunctionPermission(1005,'您没有权限添加电子围栏，请联系企业创建者！',function(){
        wx.showLoading({
          title: '处理中...',
          mask:true
        })
        // 提交添加
        requestClient.addElectronicFence(electronicFenceData,function(res){
          wx.navigateBack({
            delta: 1,
          })
        })
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  }
})